package mn.slide.labmed.database;

import mn.slide.labmed.LabMed;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ModelDbAdapter
{
	public static final String		DB_NAME					= "Labmed.sqlite";
	public static final String		TBL_NAME				= "ZMODEL";

	public static final String		FLD_MODELID				= "ZMODELID";
	public static final String		FLD_TITEL				= "ZTITEL";
	public static final String		FLD_PARENT_ID			= "ZPARENT_ID";
	public static final String		FLD_DUPLIKAT			= "ZDUPLIKAT";
	public static final String		FLD_UNTER				= "ZUNTERSUCHUNG";
	public static final String		FLD_SORT				= "ZFELD_SORT";
	public static final String		FLD_INDICATION			= "ZINDIKATION";
	public static final String		FLD_AKKREDITIERT		= "ZAKKREDITIERT";
	public static final String		FLD_INFO				= "ZINFO";
	public static final String		FLD_FELD1				= "ZFELD1";
	public static final String		FLD_FELD2				= "ZFELD2";
	public static final String		FLD_FELD3				= "ZFELD3";
	public static final String		FLD_FELD4				= "ZFELD4";
	public static final String		FLD_FELD5				= "ZFELD5";
	public static final String		FLD_FELD6				= "ZFELD6";
	public static final String		FLD_FELD7				= "ZFELD7";
	public static final String		FLD_FELD8				= "ZFELD8";
	public static final String		FLD_FELD9				= "ZFELD9";
	public static final String		FLD_FLAG1				= "ZFLAG1";
	public static final String		FLD_FLAG2				= "ZFLAG2";
	public static final String		FLD_FLAG3				= "ZFLAG3";
	public static final String		FLD_FLAG4				= "ZFLAG4";
	public static final String		FLD_FLAG5				= "ZFLAG5";
	public static final String		FLD_FLAG6				= "ZFLAG6";
	public static final String		FLD_FLAG7				= "ZFLAG7";
	public static final String		FLD_FLAG8				= "ZFLAG8";
	public static final String		FLD_FELD_SORT			= "ZFELD_SORT";
	public static final String		FLD_KONTAKTEPOOL_ID_1	= "ZKONTAKTEPOOL_ID_1";
	public static final String		FLD_KONTAKTEPOOL_ID_2	= "ZKONTAKTEPOOL_ID_2";
	public static final String		FLD_SYNONYM				= "ZSYNONYM";
	public static final String		FLD_UNTERORDNUNG		= "ZUNTERORDNUNG";
	public static final String		FLD_TEMP				= "ZTEMP";
	public static final String		FLD_VERKNUEPFUNG		= "ZVERKNUEPFUNG";
	public static final String		FLD_BEMERKUNG			= "ZBEMERKUNG";
	public static final String		FLD_ANSATZTAGE			= "ZANSATZTAGE";
	public static final String		FLD_DBID				= "ZDBID";
	public static final String		FLD_GEAENDERT			= "ZGEAENDERT";
	public static final String		FLD_FREIGABE			= "ZFREIGABE";
	public static final String		FLD_LEISTUNGSKUERZEL	= "ZLEISTUNGSKUERZEL";
	public static final String		FLD_PDF					= "ZPDF";
	public static final String		FLD_TABELLE				= "ZTABELLE";
	public static final String		FLD_BILD				= "ZBILD";
	public static final String		FLD_BEZUG				= "ZBEZUG";
	public static final String		FLD_KOMMENTAR			= "ZKOMMENTAR";
	public static final String		FLD_INT_TITEL			= "ZINT_TITEL";
	public static final String		FLD_SORTIERUNG			= "ZSORTIERUNG";
	private Context					context;
	private SQLiteDatabase			database;
	public static final String[]	COLUMNS					= new String[] { FLD_MODELID, FLD_TITEL, FLD_PARENT_ID, FLD_DUPLIKAT, FLD_INDICATION, FLD_AKKREDITIERT, FLD_INFO, FLD_UNTER, FLD_FELD1, FLD_FELD2, FLD_FELD3, FLD_FELD4, FLD_FELD5, FLD_FELD6, FLD_FELD7, FLD_FELD8, FLD_FELD9 };
	public static final String[]	COLUMNS_FOR_TITLE		= new String[] { FLD_MODELID, FLD_TITEL, FLD_PARENT_ID, FLD_UNTER, FLD_FELD1 };
	public static final String[]	COLUMNS_FOR_FELDS		= new String[] { FLD_FELD1, FLD_FELD2, FLD_FELD3, FLD_FELD4, FLD_FELD5, FLD_FELD6, FLD_FELD7, FLD_FELD8, FLD_FELD9 };
	public static final String[]	COLUMNS_FOR_ROOT		= new String[] { FLD_MODELID, FLD_PARENT_ID, FLD_DUPLIKAT, FLD_UNTER, FLD_SORT, FLD_FELD1, FLD_FELD2, FLD_FELD3, FLD_FELD4, FLD_FELD5, FLD_FELD6, FLD_FELD7, FLD_FELD8, FLD_FELD9 };

	public ModelDbAdapter( Context context )
	{
		this.setContext(context);
	}

	public long insert( String zmodel_id , String zparent_id , String zsortierung , String ztitel , String zint_titel , String zfeld1 , String zfeld2 , String zfeld3 , String zfeld4 , String zfeld5 , String zfeld6 , String zfeld7 , String zfeld8 , String zflag1 , String zflag2 , String zflag3 , String zflag4 , String zflag5 , String zflag6 , String zflag7 , String zflag8 , String zbezug , String zinfo , String zbild , String ztabelle , String zpdf , String zleistungskuerzel , String zfreigabe , String zakkreditiert , String zindikation , String zgeaendert , String zansatztage , String zbemerkung , String zverknuepfung , String zuntersuchung , String ztemp , String zunterordnung , String zsynonym , String zduplikat , String zkontaktepool_id_1 , String zkontaktepool_id_2 , String zfeld_sort )
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(FLD_MODELID, zmodel_id);
		contentValues.put(FLD_PARENT_ID, zparent_id);
		contentValues.put(FLD_SORTIERUNG, zsortierung);
		contentValues.put(FLD_TITEL, ztitel);
		contentValues.put(FLD_INT_TITEL, zint_titel);
		contentValues.put(FLD_FELD1, zfeld1);
		contentValues.put(FLD_FELD2, zfeld2);
		contentValues.put(FLD_FELD3, zfeld3);
		contentValues.put(FLD_FELD4, zfeld4);
		contentValues.put(FLD_FELD5, zfeld5);
		contentValues.put(FLD_FELD6, zfeld6);
		contentValues.put(FLD_FELD7, zfeld7);
		contentValues.put(FLD_FELD8, zfeld8);
		contentValues.put(FLD_FLAG1, zflag1);
		contentValues.put(FLD_FLAG2, zflag2);
		contentValues.put(FLD_FLAG3, zflag3);
		contentValues.put(FLD_FLAG4, zflag4);
		contentValues.put(FLD_FLAG5, zflag5);
		contentValues.put(FLD_FLAG6, zflag6);
		contentValues.put(FLD_FLAG7, zflag7);
		contentValues.put(FLD_FLAG8, zflag8);
		contentValues.put(FLD_BEZUG, zbezug);
		contentValues.put(FLD_INFO, zinfo);
		contentValues.put(FLD_BILD, zbild);
		contentValues.put(FLD_TABELLE, ztabelle);
		contentValues.put(FLD_PDF, zpdf);
		contentValues.put(FLD_LEISTUNGSKUERZEL, zleistungskuerzel);
		contentValues.put(FLD_FREIGABE, zfreigabe);
		contentValues.put(FLD_AKKREDITIERT, zakkreditiert);
		contentValues.put(FLD_INDICATION, zindikation);
		contentValues.put(FLD_ANSATZTAGE, zansatztage);
		contentValues.put(FLD_BEMERKUNG, zbemerkung);
		contentValues.put(FLD_VERKNUEPFUNG, zverknuepfung);
		contentValues.put(FLD_UNTER, zuntersuchung);
		contentValues.put(FLD_TEMP, ztemp);
		contentValues.put(FLD_UNTERORDNUNG, zunterordnung);
		contentValues.put(FLD_SYNONYM, zsynonym);
		contentValues.put(FLD_GEAENDERT, zgeaendert);
		contentValues.put(FLD_DUPLIKAT, zduplikat);
		contentValues.put(FLD_KONTAKTEPOOL_ID_1, zkontaktepool_id_1);
		contentValues.put(FLD_KONTAKTEPOOL_ID_1, zkontaktepool_id_2);
		contentValues.put(FLD_FELD_SORT, zfeld_sort);

		return database.insert(TBL_NAME, null, contentValues);
	}

	public ModelDbAdapter open() throws SQLException
	{
		database = SQLiteDatabase.openDatabase(LabMed.DATABASE_PATH, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		return this;
	}

	public int delete()
	{
		return database.delete(TBL_NAME, null, null);
	}

	public void close()
	{
		database.close();
	}

	public Cursor fetchAllTodos()
	{
		return database.query(TBL_NAME, COLUMNS, null, null, null, null, null);
	}

	public Cursor fetchTodo( long modelId ) throws SQLException
	{
		Cursor mCursor = database.query(true, TBL_NAME, COLUMNS, FLD_MODELID + "=" + modelId, null, null, null, null, null);
		if (mCursor != null)
		{
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor getParentForTitle( Cursor cursor )
	{
		if (cursor == null)
			return null;
		if (cursor.getInt(2) == 0)
		{
			return null;
		}
		Cursor mCursor = database.query(true, TBL_NAME, COLUMNS_FOR_TITLE, FLD_MODELID + "=" + cursor.getInt(2), null, null, null, null, null);
		if (mCursor != null)
		{
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor getParentByDuplicateOrParent( Cursor cursor )
	{
		String duplicate = cursor.getString(cursor.getColumnIndex(FLD_DUPLIKAT));
		Cursor mCursor;
		if (duplicate.equals("-1"))
		{
			mCursor = database.query(true, TBL_NAME, COLUMNS_FOR_ROOT, FLD_MODELID + "=" + cursor.getString(cursor.getColumnIndex(FLD_PARENT_ID)), null, null, null, null, null);
		}
		else
		{
			mCursor = database.query(true, TBL_NAME, COLUMNS_FOR_ROOT, FLD_MODELID + "=" + cursor.getString(cursor.getColumnIndex(FLD_DUPLIKAT)), null, null, null, null, null);
		}
		if (mCursor != null)
		{
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public static String[] getSortedFelds( String sort )
	{
		try
		{
			String[] sortedFelds = new String[9];
			String[] sorters = sort.split("\\|");
			int count = 0;
			for (String FELD : COLUMNS_FOR_FELDS)
			{
				int index = Integer.parseInt(sorters[count++]);
				sortedFelds[index - 1] = FELD;
			}
			return sortedFelds;
		}
		catch (Exception exception)
		{
			return COLUMNS_FOR_FELDS;
		}
	}

	public Context getContext()
	{
		return context;
	}

	public void setContext( Context context )
	{
		this.context = context;
	}
}
